var wineApp = wineApp || {};
wineApp.address = (function() {
    var resourceURL = wineApp.tool.getResourceURL();
    var params = $.url().param();
    var refUrl = params["referUrl"];
    var ajaxUrl = wineApp.CONFIG.API.address;
    var initPage = function() {
        loadAddr();
        initEvent();
    }
    var loadAddr = function() {
        wineApp.ajax.post(ajaxUrl, {}).done(function(data) {
            console.log(data);
            $.each(data.list, function(index, val) {
                /* iterate through array or object */
                var temp =
                    '<dl id="address" addrId=#addrId#>' +
                    '<dt><a href="javascript:void(0)">' +
                    '<p><span>#phone#</span>#name#</p>' +
                    '<em>#address#</em></a>' +
                    '<dd><a addrId="#addrId#" id="modify" href="newadd.html?addrId=#addrId#"><img src="../images/bz_icon.png">编辑</a><a href="#" addrId="#addrId#" class="delete"><img src="../images/del_icon.png">删除</a></dd>' +
                    '</dl>';
                var address = "";
                if (val.province != null) address += val.province;
                if (val.city != null) address += val.city;
                if (val.town != null) address += val.town;
                if (val.address != null) address += val.address;
                var param = {};
                param.phone = val.phone;
                param.name = val.recname;
                param.address = address;
                param.addrId = val.id;
                for (var prop in param) {
                    var reg = new RegExp("#" + prop + "#", "gm");
                    temp = temp.replace(reg, param[prop]);
                }
                $('.wine_add').append(temp);
            });
        })
    }
    var initEvent = function() {
        $(document).on('click', '.delete', function(e) {
            //e.preventDefault();
            var addrId = $(this).attr('addrId');
            var $this = $(this);
            var ajaxUrl = wineApp.CONFIG.API.addressdelete;
            wineApp.ajax.post(ajaxUrl, {
                id: addrId
            }).done(function(data) {
                $('.wine_add').html('');
                loadAddr();
            })
        }).on('click', '#address', function(e) {
            //e.preventDefault();
            var address = $(this).attr('addrId');
            if (!_.isNil(refUrl)) {
                var returnUrl = refUrl + "?addrId=" + address;
                window.location.href = returnUrl;
            }

        }).on('click', '.wine_newadd', function() {
            if (!_.isNil(refUrl)) {
                var returnUrl = 'newadd.html?referUrl=' + refUrl;
                window.location.href = returnUrl;
            } else {
                window.location.href = 'newadd.html';
            }
        })
    }

    return {
        initPage: initPage
    }
})()

$(document).ready(function() {
    $('.wine_footer').remove();
    wineApp.common.initHeaderIcon(0, true, {
        camera: false,
        search: false
    }, 'my_account.html');
    wineApp.address.initPage();
});